package com.huaxin.hxmodulesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxin.hxmodulesystem.dto.ResponseCode;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddPermissionReqDTO;
import com.huaxin.hxmodulesystem.dto.request.UpdatePermissionReqDTO;
import com.huaxin.hxmodulesystem.mapper.PermissionMapper;
import com.huaxin.hxmodulesystem.mapper.UserRoleMapper;
import com.huaxin.hxmodulesystem.mapping.PermissionMapping;
import com.huaxin.hxmodulesystem.models.Permission;
import com.huaxin.hxmodulesystem.service.PermissionService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    private final CacheManager cacheManager;

    private final ObjectMapper objectMapper = new ObjectMapper();

    private final ApplicationContext applicationContext;

    private final PermissionMapping permissionMapping;

    private final UserRoleMapper userRoleMapper;

    /**
     * 新增权限
     *
     * @param in 入参
     * @return
     */
    @Override
    public ResponseResult<String> addPermission(AddPermissionReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数错误");
        }
        if (StringUtils.isBlank(in.getPermissionKey())) {
            return ResponseResult.parameterError(null, "权限标识不能为空");
        }
        if (in.getType() == null || in.getType() <= 0) {
            return ResponseResult.parameterError(null, "权限类型不能为空");
        }
        if (in.getApplicationType() == null || in.getApplicationType() <= 0) {
            return ResponseResult.parameterError(null, "应用类型不能为空");
        }
        Permission entity = permissionMapping.toEntity(in);
        if (this.save(entity)) {
            return ResponseResult.ok(null);
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "添加权限失败，请稍后再试！", null);
        }
    }

    /**
     * 修改权限
     *
     * @param in 入参
     * @return
     */
    @Override
    public ResponseResult<String> updatePermission(UpdatePermissionReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数错误");
        }
        if (StringUtils.isBlank(in.getId())) {
            return ResponseResult.parameterError(null, "权限ID不能为空");
        }
        if (in.getType() == null || in.getType() <= 0) {
            return ResponseResult.parameterError(null, "权限类型不能为空");
        }
        if (in.getApplicationType() == null || in.getApplicationType() <= 0) {
            return ResponseResult.parameterError(null, "应用类型不能为空");
        }
        Permission entity = this.getById(in.getId());
        permissionMapping.updateEntity(in, entity);
        if (this.updateById(entity)) {
            return ResponseResult.ok(null);
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "修改权限失败，请稍后再试！", null);
        }
    }

    /**
     * 删除权限
     *
     * @param id 权限ID
     * @return
     */
    @Override
    public ResponseResult<String> deletePermission(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "权限ID不能为空");
        }
        if (applicationContext.getBean(PermissionService.class).removeById(id)) {
            return ResponseResult.ok(null);
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "删除权限失败，请稍后再试！", null);
        }
    }

    /**
     * 查询权限
     *
     * @param id 权限ID
     * @return
     */
    @Override
    public ResponseResult<Permission> queryPermission(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "权限ID不能为空");
        }
        return ResponseResult.ok(this.getById(id));
    }

    /**
     * 查询权限列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @param search   搜索条件
     * @return
     */
    @Override
    public ResponseResult<Page<Permission>> queryPermissionList(Integer pageNum, Integer pageSize, String search) {
        Page<Permission> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            wrapper.like("permission_name", search)
                    .or()
                    .like("permission_code", search)
                    // 可继续添加更多字段
                    .or()
                    .like("description", search);
        }
        return ResponseResult.ok(this.baseMapper.selectPage(page, wrapper));
    }


}